<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BDataObjContext
 *
 * @author I062261
 */
class BRepositoryContext {

    //put your code here
    public $id = null;
    public $name = null;
    public $description = null;
    public $spaceId = null;
    public $appId = null;
    public $entity = null;
    public $entityClass = null;
    public $modelClass = null;
    public $statusId = null;
    public $groupName = null;
    public $memberName = null;
    public $groupClass = null;
    public $memberClass = null;
    public $groupModelClass = null;
    public $groupMemberModelClass = null;
    public $modelChildClass = null;
    public $data = null;
    public $dataObj = null;
    public $parent = null;
    public $saveItems = array();
    public $loadItems = array();
    public $findCondition = null;
    public $findConditions = array();
    public $findParams = array();

    public function __construct($caller, $data = NULL) {
        if ($caller instanceof BEntity) {
            $this->entity = $caller;
            $this->entityClass = get_class($caller);
            //$this->parseSaveItems($this->entity);
        } else {
            $this->entityClass = $caller;
        }

        if ($this->entityClass == NULL)
            $this->entityClass = get_called_class();

        if (!$this->entity instanceof BEntity) {
            $class = $this->entityClass;
            if ($class != 'BRepositoryContext')
                $this->entity = new $class();
        }

        $this->parseData($data);
    }

    public function setData($data) {
        $this->parseData($data);
    }

    public function setEntity($entity, $data = NULL) {
        if ($entity instanceof BEntity) {

            $this->entity = $entity;
            $this->entityClass = get_class($entity);
            //$this->parseSaveItems($entity);
            $this->parseData($data);
        }
    }

    public function init($data = NULL) {

        if (is_array($data) && $data != NULL) {
            if (isset($data['callerClass'])) {
                $this->entityClass = $data['callerClass'];
            }
        }

        if (is_array($data) && $data != NULL) {
            if (isset($data['entityClass'])) {
                $this->entityClass = $data['entityClass'];
            }
        }

        if ($this->entity instanceof BEntity) {
            if ($this->entity instanceof BApp) {
                $this->modelClass = 'BMApp';
            }
            else if ($this->entity instanceof BSpace) {
                $this->modelClass = 'BMSpace';
            }
            else if ($this->entity instanceof BOperation) {
                $this->modelClass = 'BMOperation';
            }
            else if ($this->entity instanceof BOperationFlow) {
                $this->modelClass = 'BMOperationflow';
            }
            else {
                $pos = strpos(get_class($this->entity), "BS");
                if($pos === 0)
                    $this->modelClass = preg_replace("/^BS/", "BSM", get_class($this->entity)); //for bookingSport model!
                else
                    $this->modelClass = preg_replace("/^B/", "BM", get_class($this->entity));
            }
        }
        else
        {
             $pos = strpos($this->entityClass, "BS");
                if($pos === 0)
                    $this->modelClass = preg_replace("/^BS/", "BSM", $this->entityClass); //for bookingSport model!
                else
                    $this->modelClass = preg_replace("/^B/", "BM", $this->entityClass);
        }

        if ($this->entity instanceof BGroup) {
            $this->memberClass = str_replace('Group', '', get_class($this->entity));
            $pos = strpos($this->memberClass, "BS");
                if($pos === 0)
                    $this->groupMemberModelClass = preg_replace("/^BS/", "BSM", $this->entityClass . 'Member'); //for bookingSport model!
                else
                    $this->groupMemberModelClass = preg_replace("/^B/", "BM", $this->entityClass . 'Member');
        }

        if ($this->entity instanceof BEntityWithGroup) {
            if ($this->entity instanceof BApp) {
                $this->groupClass = 'BAppGroup';
            } else {
                $this->groupClass = $this->entityClass . 'Group';
                if (is_array($data) && isset($data['groupClass'])) {
                    $this->groupClass = $data['groupClass'];
                }
            }
            $pos = strpos(($this->groupClass), "BS");
                if($pos === 0)
                    $this->groupModelClass = preg_replace("/^BS/", "BSM", ($this->groupClass)); //for bookingSport model!
                else
                    $this->groupModelClass = preg_replace("/^B/", "BM", $this->groupClass);

            if (preg_match("/Group/", $this->groupClass))
                $this->groupMemberModelClass = $this->groupModelClass . 'Member';
            else {
                $memberClass = get_class($this->entity);
                $pos = strpos(($this->$memberClass), "BS");
                if($pos === 0)
                    $this->groupMemberModelClass = $this->groupModelClass . preg_replace("/^BS/", "", $memberClass); //for bookingSport model!
                else                
                $this->groupMemberModelClass = $this->groupModelClass . preg_replace("/^B/", "", $memberClass);
            }
        }

        if ($this->entity instanceof BRelationshipEntity) {
            $this->saveItems = array();
            $pos = strpos(get_class($this->entity), "BS");
                if($pos === 0)
                    $this->modelClass = preg_replace("/^BS/", "BSM", get_class($this->entity->mainObj)); //for bookingSport model!
                else
            $this->modelClass = preg_replace("/^B/", "BM", get_class($this->entity->mainObj));
            $mainObjClass = get_class($this->entity->mainObj);
            $mainObjClass = preg_replace("/Group$/", '', $mainObjClass);
            $mainObjCol = strtolower(substr($mainObjClass, 1)) . "_id";
            $mainObjValue = $this->entity->mainObj->getId();
            if ($this->entity->mainObj instanceof BGroup) {
                $this->modelClass = preg_replace("/Group$/", '', $this->modelClass);
            }
            if ($this->entity->mainObj instanceof BApp || $this->entity->mainObj instanceof BAppGroup) {
                $this->modelClass = 'BMApp';
            }

            if ($this->entity->pairObj instanceof BEntity) {
                if ($this->entity->pairObj instanceof BApp || $this->entity->pairObj instanceof BAppGroup) {
                    $this->modelClass .= 'App';
                } else {
                    $pos = strpos(get_class($this->entity->pairObj), "BS");
                    if($pos === 0)
                        $this->modelClass .= preg_replace("/^BS/", "", get_class($this->entity->pairObj)); //for bookingSport model!
                    else
                    $this->modelClass .= preg_replace("/^B/", "", get_class($this->entity->pairObj));
                    $this->modelClass = preg_replace("/Group$/", '', $this->modelClass);
                }
            }
            //currently, only BMResourcePermitRole have three objects for relationship
            if (is_array($this->entity->pairObjs) && $this->entity->pairObjs != NULL) {
                $this->modelClass = 'BMResourcePermitRole';
            }
        }
    }

    public function getModelClass($data = NULL) {

        if (isset($data) && is_string($data))
        {
            $pos = strpos($data, "BS");
            if($pos === 0)
               preg_replace("/^BS/", "BSM", $data); //for bookingSport model!
            else
                return preg_replace("/^B/", "BM", $data);
        }

        $this->parseData($data);
        return $this->modelClass;
    }

    public function parseData($data = NULL) {

        $this->init($data);

        //save array data with property into entity
        if (is_array($data)) {
            $entity = $this->entity;
            if ($entity instanceof BEntity) {
                $mapList = $entity->getPropertyToColumnMapList();
                foreach ($mapList as $property => $col) {
                    if (isset($data[$property]) && $data[$property] != NULL)
                        $entity->$property = $data[$property];
                }
            }
        }

        //save entity of data into entity obj
        if ($data instanceof BEntity) {
            $entity = $this->entity;
            $class = get_class($entity);
            if ($entity instanceof $class) {
                $mapList = $entity->getPropertyToColumnMapList();
                foreach ($mapList as $property => $col) {
                    if (property_exists($class, $property) && $data->$property != NULL)
                        $entity->$property = $data->$property;
                }
            }
        }

        if (is_array($data) && $data != NULL) {
            if (isset($data['modelClass'])) {
                $this->modelClass = $data['modelClass'];
            }
        }

        //by default, status of "object_deleted" set as default status
        $mStatus = BMStatus::model()->findByAttributes(array('name' => BStatus::Status_Object_Deleted));
        if ($mStatus instanceof BMStatus)
            $this->statusId = $mStatus->_id;
        else {
            $mStatus = new BMStatus();
            $mStatus->name = BStatus::Status_Object_Deleted;
            $mStatus->save();
            $this->statusId = $mStatus->_id;
        }

        if (is_array($data) && $data != NULL) {
            if (isset($data['statusId'])) {
                $this->statusId = $data['statusId'];
            }
        }

        if ($data instanceof BEntity) {
            if (property_exists(get_class($data), 'name'))
                $this->name = $data->name;
        }

        if (is_numeric($data))
            $this->id = $data;

        if (is_string($data))
            $this->name = $data;

        if (is_array($data) && $data != NULL) {
            if (isset($data['name'])) {
                $this->name = $data['name'];
            }
        }

        if (is_array($data) && $data != NULL) {
            if (isset($data['id'])) {
                $this->id = $data['id'];
            }
        }

        if (is_array($data) && $data != NULL) {
            if (isset($data['description'])) {
                $this->description = $data['description'];
            }
        }

        if (is_array($data) && $data != NULL) {
            if (isset($data['data'])) {
                $this->data = $data['data'];
            }
        }

        if ($this->name != NULL) {
            $names = preg_split('[' . BTreeEntity::delimiter . ']', $this->name);
            $count = count($names);
            if ($count > 1) {
                $this->groupClass = $this->entityClass . 'Group';
                $this->groupMemberModelClass = $this->groupModelClass . 'Member';
                $this->memberName = $names[$count - 1];
                if (is_array($data) && isset($data['groupClass']))
                    $this->groupClass = $data['groupClass'];
                unset($names[$count - 1]);
                $groupName = '';
                $i = 0;
                foreach ($names as $name) {
                    $groupName.=$name;
                    if (++$i == $count - 1)
                        break;
                    $groupName.= '.';
                }
                $this->groupName = $groupName;
            }
        }

        if (is_array($data) && $data != NULL) {
            if (isset($data['spaceId'])) {
                $this->spaceId = $data['spaceId'];
            }
        }

        if (is_array($data) && $data != NULL) {
            if (isset($data['appId'])) {
                $this->appId = $data['appId'];
            }
        }

        if (is_array($data) && $data != NULL) {
            if (isset($data['parent'])) {
                $parent = $data['parent'];
                $this->parent = $parent;
            }
        }

        if (is_array($data) && $data != NULL) {
            if (isset($data['memberClass'])) {
                $this->memberClass = $data['memberClass'];
            }
        } else {
            $this->memberClass = str_replace('Group', '', $this->entityClass);
        }

        if (is_array($data) && $data != NULL) {
            if (isset($data['findConditions'])) {
                $this->findConditions = $data['findConditions'];
            }
        }

        if (is_array($data) && $data != NULL) {
            if (isset($data['saveItems'])) {
                $this->saveItems = $data['saveItems'];
            }
        }

        $this->addSaveItems($this->entity);
        $this->parseFindConditions($this->entity);
        $this->parseLoadItems($this->entity);
    }

    public function splitTreeName($name = NULL) {

        if (!isset($name))
            $name = $this->name;

        $names = preg_split("/[.\\/|]/", $name);

        return $names;
    }

    public function parseFindConditions($data = NULL) {

        $findConditions = $this->findConditions;
        //$findParams = $this->findParams;

        if ($data instanceof BEntity) {
            $entity = $data;
            $mapList = $entity->getPropertyToColumnMapList();

            foreach ($mapList as $property => $col) {
                if ($entity->$property != NULL)
                    $findConditions[$col] = $entity->$property;
            }
        }

        if (is_array($data)) {
            $class = $this->entityClass;
            $entity = new $class();
            if ($entity instanceof BEntity) {
                $mapList = $entity->getPropertyToColumnMapList();
                foreach ($mapList as $property => $col) {
                    if (isset($data[$property]) && $data[$property] != NULL)
                        $findConditions[$col] = $data[$property];
                }
            }
        }

        $this->findConditions = $findConditions;
        return $findConditions;
    }

    public function addSaveItems($data = NULL) {

        $saveItems = $this->saveItems;
        if ($data instanceof BEntity) {
            $entity = $data;
            $mapList = $entity->getPropertyToColumnMapList();

            foreach ($mapList as $property => $col) {
                if ($entity->$property != NULL)
                    $saveItems[$col] = $entity->$property;
            }
        }

        if (is_array($data)) {
            $class = $this->entityClass;
            $entity = new $class();
            if ($entity instanceof BEntity) {
                $mapList = $entity->getPropertyToColumnMapList();
                foreach ($mapList as $property => $col) {
                    if (isset($data[$property]) && $data[$property] != NULL)
                        $saveItems[$col] = $data[$property];
                }
            }
        }

        $this->saveItems = $saveItems;
        return $saveItems;
    }

    public function parseLoadItems($data = NULL) {

        if ($data instanceof BEntity) {
            $entity = $data;
            $mapList = $entity->getPropertyToColumnMapList();
            $this->loadItems = $mapList;
            return $mapList;
        }
    }

}

?>
